﻿using System;
using System.Windows.Forms;

namespace OKDebug
{
    public partial class FrmModbus : Form
    {
        public string MESSAGE { get; set; }
        public FrmModbus()
        {
            InitializeComponent();
        }

        private void btnBuildMsg_Click(object sender, EventArgs e)
        {
            string devAddr = txtDevAddr.Text.Trim();
            string funCode = txtFunCode.Text.Trim();
            string addr = txtAddress.Text.Trim();
            string length = txtLength.Text.Trim();

            int _devAddr = 0;
            if (string.IsNullOrEmpty(devAddr))
            {
                MessageBox.Show("设备地址空！");
                return;
            }
            else
            {
                try
                {
                    _devAddr = Convert.ToInt32(devAddr);
                }
                catch
                {
                    MessageBox.Show("设备地址格式不对！");
                    return;
                }
            }

            byte _funCode = 0;
            if (string.IsNullOrEmpty(funCode))
            {
                MessageBox.Show("功能码空！");
                return;
            }
            else
            {
                try
                {
                    _funCode = Convert.ToByte("0x" + funCode, 16);
                }
                catch
                {
                    MessageBox.Show("功能码格式不对！");
                    return;
                }
            }

            int _addr = 0;
            if (string.IsNullOrEmpty(addr))
            {
                MessageBox.Show("寄存器地址空！");
                return;
            }
            else
            {
                try
                {
                    _addr = Convert.ToInt32(addr);
                }
                catch
                {
                    MessageBox.Show("寄存器地址格式不对！");
                    return;
                }
            }

            int _length = 0;
            if (string.IsNullOrEmpty(length))
            {
                MessageBox.Show("长度空！");
                return;
            }
            else
            {
                try
                {
                    _length = Convert.ToInt32(length);
                }
                catch
                {
                    MessageBox.Show("长度格式不对！");
                    return;
                }
            }

            byte[] buffer = BuildMessage(_devAddr, _funCode,_addr,_length);
            MESSAGE = BitConverter.ToString(buffer);
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 构造发送报文
        /// </summary>
        /// <param name="devAddr">设备地址</param>
        /// <param name="funcode">功能码</param>
        /// <param name="data">数据</param>
        /// <param name="addr">地址</param>
        /// <param name="length">变量个数</param>
        /// <returns></returns>
        private static byte[] BuildMessage(int devAddr, byte funCode, int addr, int length)
        {
            int crcVal;
            byte[] headData = new byte[6];
            headData[0] = Convert.ToByte(devAddr);//设备地址
            headData[1] = (byte)(funCode & 0xFF);//功能码 
            headData[2] = (byte)(addr >> 8);//寄存器地址第一个字节 >>8表示二进制向右移动8位，丢弃最右边的8位，并在最左边补上0
            headData[3] = (byte)(addr & 0xFF);//寄存器地址第二个字节
            headData[4] = (byte)(length >> 8);//寄存器个数第一个字节
            headData[5] = (byte)(length & 0xFF);//寄存器个数第二个字节
            byte[] messageData = new byte[headData.Length + 2];
            headData.CopyTo(messageData, 0);
            crcVal = CRC.Crc16(messageData, messageData.Length - 2);
            messageData[messageData.Length - 2] = (byte)(crcVal & 0xFF);//校验码第一个字节
            messageData[messageData.Length - 1] = (byte)(crcVal >> 8);//校验码第二个字节
            return messageData;
        }
    }
}
